﻿using AutoMapper;
using Harmontronics.DataAccess.IOperate.Business;
using Harmontronics.DataAccess.Operate.Business;
using Harmontronics.IService;
using Harmontronics.IService.Business;
using Harmontronics.Model.Dto;
using Harmontronics.Model.Dto.Model;
using Harmontronics.Model.Entitys;
using Harmontronics.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Harmontronics.Service
{
    public class UserRecipeService : IUserRecipeService
    {
        private readonly IUserRecipeOperate _userRecipeOperate;
        private readonly IRecipeConfigOperate _recipeConfigOperate;
        private readonly IMapper _mapper;
        private readonly IUnitOfWork _unitOfWork;
        public UserRecipeService(IUnitOfWork unitOfWork, IMapper mapper, IUserRecipeOperate userRecipeOperate, IRecipeConfigOperate recipeConfigOperate)
        {
            _mapper = mapper;
            _unitOfWork = unitOfWork;
            _userRecipeOperate = userRecipeOperate;
            _recipeConfigOperate = recipeConfigOperate;
        }

        public List<UserRecipeModel> MendingRange(List<UserRecipe> entities)
        {
            List<UserRecipeModel> models = new List<UserRecipeModel>();
            foreach (var entity in entities)
            {
                models.Add(_mapper.Map<UserRecipeModel>(entity));
            }
            return models;
        }

        public UserRecipeModel Mending(UserRecipe entity)
        {
            UserRecipeModel model = _mapper.Map<UserRecipeModel>(entity);
            if (entity.ParameterId != null)
            {
                RecipeConfig config = _recipeConfigOperate.GetRecipeConfigById(entity.ParameterId);
                model.RecipeConfig = config;
            }
            else
            {
                model.RecipeConfig = new RecipeConfig()
                {
                    ParameterId = entity.ParameterId
                };
            }
            return _mapper.Map<UserRecipeModel>(entity); ;
        }
        public List<UserRecipe> GetAllUserRecipes()
        {
            try
            {
                return _userRecipeOperate.GetAllUserRecipeList();
            }
            catch (Exception ex)
            {
                return new List<UserRecipe>();
            }
        }

        public bool CheckRepeated(UserRecipe entity)
        {
            if (entity != null)
            {
                UserRecipe userRecipe = _userRecipeOperate.GetUserRecipeByIds(entity.RecipeId, entity.ParameterId);
                if (userRecipe != null)
                {
                    return true;
                }
            }
            return false;
        }

        public UserRecipe GetUserRecipeByIds(string recipeId, string paramterId)
        {
            try
            {
                UserRecipe userRecipes = _userRecipeOperate.GetUserRecipeByIds(recipeId, paramterId);
                return userRecipes;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public bool addUserRecipe(UserRecipe entity)
        {
            try
            {
                _userRecipeOperate.AddUserRecipe(entity);
                _unitOfWork.Commit();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool updateUserRecipe(UserRecipe model)
        {
            try
            {
                UserRecipe entity = _userRecipeOperate.GetUserRecipeByIds(model.RecipeId, model.ParameterId);
                _mapper.Map(model, entity);
                _userRecipeOperate.UpdateUserRecipe(entity);
                _unitOfWork.Commit();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool DeleteUserRecipe(string recipeId, string parmaterId)
        {
            try
            {
                UserRecipe entity = _userRecipeOperate.GetUserRecipeByIds(recipeId, parmaterId);
                if (entity != null)
                {
                    _userRecipeOperate.DeleteUserRecipe(entity);
                    _unitOfWork.Commit();
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool BatchDelUserRecipe(List<UserRecipe> Deleted)
        {
            try
            {
                List<string> recipeIds = Deleted.Select(x => x.RecipeId).Distinct().ToList();
                List<string> parmaterIds = Deleted.Select(x => x.ParameterId).Distinct().ToList();
                List<UserRecipe> entities = _userRecipeOperate.GetUserRecipeByIdList(recipeIds, parmaterIds);
                if (entities.Count > 0)
                {
                    _userRecipeOperate.BatchDelUserRecipe(entities);
                    _unitOfWork.Commit();
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
